package com.xingxuecn.limit.web.action.sys;

import com.opensymphony.xwork2.ActionSupport;
import com.xingxuecn.limit.common.model.vo.Page;
import com.xingxuecn.limit.common.model.vo.ResultModel;
import com.xingxuecn.limit.common.model.dto.Role;
import com.xingxuecn.limit.common.utils.ResponseUtils;
import com.xingxuecn.limit.service.RoleService;
import com.xingxuecn.limit.service.impl.RoleServiceImpl;

import org.apache.struts2.ServletActionContext;

import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Kris at 2018/9/10 16:47
 */
public class RoleAction extends ActionSupport {

    RoleService roleService = new RoleServiceImpl();


    private Integer page;
    private Integer rows;
    private Role role = new Role();


    public String index(){
        return SUCCESS;
    }


    public String search() throws SQLException {
        Page<Role> result = roleService.search(role,page,rows);
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(result,response);
        return  null;
    }

    public String add(){
        ResultModel result;
        //1.调用业务逻辑层实现新增的逻辑
        try {
            int i = roleService.add(role);
            if(i>0){
                result = new ResultModel(200,"新增成功");
            }else{
                result = new ResultModel(500,"新增失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            result = new ResultModel(500,"新增失败");
        }
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(result,response);
        return  null;
    }

    /**
     * 修改
     * @return
     */
    public String update(){
        ResultModel result;
        //1.调用业务逻辑层实现新增的逻辑
        try {
            int i = roleService.update(role);
            if(i>0){
                result = new ResultModel(200,"修改成功");
            }else{
                result = new ResultModel(500,"修改失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            result = new ResultModel(500,"修改失败");
        }
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(result,response);
        return  null;
    }


    public String delete(){
        ResultModel result;
        try {
            roleService.delete(role.getId());
            result = new ResultModel(200,"删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
            result = new ResultModel(500,"删除失败");
        }
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(result,response);
        return  null;
    }


    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}
